/*
 * BangoStatic.cpp
 *
 *  Created on: 27 Dec 2012
 */

#include "BangoStatic.h"

	deviceinfo_identifying_details_t *id_details = NULL;
	int rc = deviceinfo_get_identifying_details(&id_details);
	deviceinfo_details_t *device_details = NULL;
	int dd = deviceinfo_get_details(&device_details);

QNetworkAccessManager * BangoStatic::StaticNetworkAccessManager()
{
	static QNetworkAccessManager *qnam = new QNetworkAccessManager();
	return qnam;
}
BangoStatic::BangoStatic(QObject *parent) : QObject(parent)
{

}

BangoStatic::~BangoStatic()
{

}

bool BangoStatic::IsRoaming()
{
	qDebug() << __PRETTY_FUNCTION__;
	CellularNetworkInfo ci;

	bool isRoaming = ci.isRoaming();
	return isRoaming;
}


QString BangoStatic::GetUserID()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString tempString;
	HardwareInfo hw;
	QString imei = hw.imei();

	if(!(imei.isEmpty() || imei.isNull()))
	{
		tempString = QCryptographicHash::hash(hw.imei().toAscii(), QCryptographicHash::Md5).toHex();
	}

	return tempString;


}

QDateTime BangoStatic::GetUTCDate()
{
	QDateTime date = QDateTime::currentDateTimeUtc();
	return date;
}

QString BangoStatic::GetOperatingSystemName()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString operatingSystemName;

	if(BPS_SUCCESS == dd)
	{
		operatingSystemName = deviceinfo_details_get_device_os(device_details);
		deviceinfo_free_details(&device_details);
	}
	return operatingSystemName;
}


QString BangoStatic::GetOperatingSystemVersion()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString operatingSystemVersion;
	PlatformInfo pi;

	operatingSystemVersion = pi.osVersion();
	return operatingSystemVersion;
}

/**
 * Until the BB10 libraries add support for QtWebKit
 * the webkit version number must be hard coded.
 */

QString BangoStatic::GetUserAgent()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString userAgent;
	QString webkitVersion;
	userAgent += QString::fromLatin1("Mozilla/5.0 (BB10; ");
	HardwareInfo hw;

	userAgent += hw.modelName();
	userAgent += ")";

	webkitVersion += QString::fromLatin1(" AppleWebKit/537.10+ ");


	userAgent += webkitVersion;
	userAgent += QString::fromLatin1("(KHTML, like Gecko) Version/");
	QString modelNumber = hw.modelNumber();
	userAgent += modelNumber;
	userAgent += QString::fromLatin1("Mobile Safari/537.10+");

	return userAgent;

}

bool BangoStatic::IsWiFiConnection()
{
	qDebug() << __PRETTY_FUNCTION__;
	bool isWifi = false;

	QNetworkAccessManager *	qnam = StaticNetworkAccessManager();
	QNetworkConfiguration activeConfig = qnam->activeConfiguration();

	switch(activeConfig.bearerType())
	{
	case QNetworkConfiguration::BearerEthernet:
	case QNetworkConfiguration::BearerWiMAX:
	case QNetworkConfiguration::BearerWLAN:
	case QNetworkConfiguration::BearerBluetooth:
		isWifi = true;
		break;
	default:
		isWifi = false;
		break;
	}
	return isWifi;
}

QString BangoStatic::ConnectionType(int connectionType)
{
	qDebug() << __PRETTY_FUNCTION__;
	QString returnValue = "OTHER";

	if(connectionType == BangoConstants::GetInstance()->WIFI_CONNECTION){returnValue = "WIFI";}
	if(connectionType == BangoConstants::GetInstance()->OPERATOR_CONNECTION){returnValue = "OPERATOR";}
	return returnValue;
}

int BangoStatic::GetConnectionType()
{
	qDebug() << __PRETTY_FUNCTION__;
	int connType = BangoConstants::GetInstance()->UNKNOWN_CONNECTION;
	QNetworkAccessManager *qnam = StaticNetworkAccessManager();
	QNetworkConfiguration activeConfig = qnam->activeConfiguration();

	switch(activeConfig.bearerType())
	{
	case QNetworkConfiguration::Bearer2G:
	case QNetworkConfiguration::BearerCDMA2000:
	case QNetworkConfiguration::BearerWCDMA:
	case QNetworkConfiguration::BearerHSPA:
		connType = BangoConstants::GetInstance()->OPERATOR_CONNECTION;
		break;
	case QNetworkConfiguration::BearerEthernet:
	case QNetworkConfiguration::BearerWiMAX:
	case QNetworkConfiguration::BearerWLAN:
	case QNetworkConfiguration::BearerBluetooth:
	    connType = BangoConstants::GetInstance()->WIFI_CONNECTION;
	    break;
	case QNetworkConfiguration::BearerUnknown:
	    connType = BangoConstants::GetInstance()->UNKNOWN_CONNECTION;
	    break;
	}
	return connType;
}

QString BangoStatic::GetOperatorConnectionType()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString opConnType = BangoConstants::GetInstance()->OPTION_OTHER;

	QNetworkAccessManager *qnam = StaticNetworkAccessManager();
	QNetworkConfiguration activeConfig = qnam->activeConfiguration();

	switch(activeConfig.bearerType())
	{
	case QNetworkConfiguration::Bearer2G:
		opConnType = activeConfig.bearerName();
		break;
	case QNetworkConfiguration::BearerCDMA2000:
		opConnType = "CDMA";
		break;
	case QNetworkConfiguration::BearerWCDMA:
		opConnType = "UMTS";
		break;
	case QNetworkConfiguration::BearerHSPA:
		opConnType = "HSPA";
		break;
	case QNetworkConfiguration::BearerEthernet:
	case QNetworkConfiguration::BearerWiMAX:
	case QNetworkConfiguration::BearerWLAN:
	case QNetworkConfiguration::BearerBluetooth:
	case QNetworkConfiguration::BearerUnknown:
		opConnType = BangoConstants::GetInstance()->OPTION_OTHER;
		break;
	}
	return opConnType;
}

QString BangoStatic::GetCountryISO()
{
	qDebug() << __PRETTY_FUNCTION__;
	QString countryCode;
	QtMobilitySubset::QGeoAddress *add = new QtMobilitySubset::QGeoAddress();
	countryCode = add->countryCode();
	return countryCode;
}
